<?php
/**
 * 云片网短信发送接口
 * User: yu
 * Date: 16-11-14
 * Time: 下午3:40
 */
namespace utils\sms;

class YunPianProvider
{
    private $user = null;
    private $pass = null;
    private $key = null; //$apikey 修改为您的apikey(https://www.yunpian.com)登陆官网后获取
    private $sign = null;
    private $authed = false;

    public function auth($user, $pass, $key, $sign)
    {
        $this->user = $user;
        $this->pass = $pass;
        $this->key = $key;
        $this->sign = $sign;
        $this->authed=true;
    }

    /**
     * @param $mobile 请用自己的手机号代替
     * @param $content //【云片网】您的验证码是1234
     */
    public function send($mobile,$content)
    {
        if($this->authed!=true){

            return false;
        }

        header("Content-Type:text/html;charset=utf-8");

        $ch = curl_init();

        /* 设置验证方式 */

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 发送短信
        $data = array('text' => $content, 'apikey' => $this->key, 'mobile' => $mobile);
        $json_data = $this->text_send($ch, $data);
        $array = json_decode($json_data, true);
        echo '<pre>';
        print_r($array);

        curl_close($ch);

        return $array;


    }

    //获得账户
    public function get_user($ch, $apikey)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        return curl_exec($ch);
    }

    public function text_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    public function tpl_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    public function voice_send($ch, $data)
    {
        curl_setopt($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
}

?>